/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatv2.client;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.text.DefaultCaret;

import rmioverlay.client.Client;
import chatv2.client.ChatClientApp;

/**
 *
 * @author Aldok-EEE
 */
public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form ConfigurationGUI
     */
    public GUI() {
        initComponents();
        try {
        	app = new ChatClientApp(this);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        //jButton3 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel_Conf = new javax.swing.JPanel();
        jLabel_Conf_Ip = new javax.swing.JLabel();
        jTextField_Conf_Ip = new javax.swing.JTextField();
        jLabel_Conf_Port = new javax.swing.JLabel();
        jTextField_Conf_Port = new javax.swing.JTextField();
        jButton_Conf_Connect = new javax.swing.JButton();
        jLabel_Conf_Title = new javax.swing.JLabel();
        jScrollPane_Conf_Logs = new javax.swing.JScrollPane();
        jTextArea_Conf_Logs = new javax.swing.JTextArea();
        jLabel_Conf_Logs = new javax.swing.JLabel();
        jButton_Conf_Disconnect = new javax.swing.JButton();
        jTextField_Conf_Login = new javax.swing.JTextField();
        jLabel_Conf_Login = new javax.swing.JLabel();
        jLabel_Conf_Password = new javax.swing.JLabel();
        jPasswordField_Conf_Password = new javax.swing.JPasswordField();
        jPanel_Chat = new javax.swing.JPanel();
        jTabbed_Chat = new javax.swing.JTabbedPane();
        jPanel_ChatMain = new javax.swing.JPanel();
        jScrollPane_ChatMain_Main = new javax.swing.JScrollPane();
        jTextArea_ChatMain_Main = new javax.swing.JTextArea();
        jScrollPane_ChatMain_Send = new javax.swing.JScrollPane();
        jTextArea_ChatMain_Send = new javax.swing.JTextArea();
        jButton_SendChatMain = new javax.swing.JButton();
        jScrollPane_ChatMain_Members = new javax.swing.JScrollPane();
        jList_ChatMain_Members = new javax.swing.JList();
        //jPanel_Whisper = new javax.swing.JPanel();
        jScrollPane_ChatWhisper_Main = new javax.swing.JScrollPane();
        jTextArea_ChatWhisper_Main = new javax.swing.JTextArea();
        jScrollPane_ChatWhisper_Send = new javax.swing.JScrollPane();
        jTextArea_ChatWhisper_Send = new javax.swing.JTextArea();
        jButton_ChatWhisper_SendFile = new javax.swing.JButton();
        jButton_ChatWhisper_Send = new javax.swing.JButton();
        jButton_ChatMain_SendFile = new javax.swing.JButton();
        
        jButton_Conf_Disconnect.setEnabled(false);

        //jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel_Conf_Ip.setText("IP");

        jTextField_Conf_Ip.setText("127.0.0.1");

        jLabel_Conf_Port.setText("Port");

        jTextField_Conf_Port.setText("1099");
        
        this.addWindowListener(new WindowAdapter() {
        	public void windowClosing(WindowEvent ev) {
        		try {
					app.disconnect();
				} catch (RemoteException e) {}
        		finally {
            		System.exit(0);
        		}
        	}
		});
        
        /*jTextField_Conf_Port.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_Conf_PortActionPerformed(evt);
            }
        });*/

        jButton_Conf_Connect.setText("Connect");
        jButton_Conf_Connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Conf_ConnectActionPerformed(evt);
            }
        });

        jLabel_Conf_Title.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
        jLabel_Conf_Title.setText("Chat temoin");

        jTextArea_Conf_Logs.setBackground(new java.awt.Color(230, 230, 230));
        jTextArea_Conf_Logs.setColumns(20);
        jTextArea_Conf_Logs.setEditable(false);
        jTextArea_Conf_Logs.setRows(5);
        jScrollPane_Conf_Logs.setViewportView(jTextArea_Conf_Logs);

        jLabel_Conf_Logs.setText("Logs");

        jButton_Conf_Disconnect.setText("Disconnect");
        jButton_Conf_Disconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Conf_DisconnectActionPerformed(evt);
            }
        });

       /* jTextField_Conf_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_Conf_LoginActionPerformed(evt);
            }
        });*/

        jLabel_Conf_Login.setText("Login");

        jLabel_Conf_Password.setText("Password");
        jButton_ChatMain_SendFile.setText("File");

        jPasswordField_Conf_Password.setText("");
        /*jPasswordField_Conf_Password.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField_Conf_PasswordActionPerformed(evt);
            }
        });*/
        
        jButton_ChatMain_SendFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	try {
					jButton_ChatMain_SendFileActionPerformed(evt);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        
        
        jList_ChatMain_Members.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                if (evt.getClickCount() == 2) {
                	jList_ChatMain_MembersMouseListener(evt);
                    
                } else if (evt.getClickCount() == 3) {   // Triple-click
                	jList_ChatMain_MembersMouseListener(evt);
                }
            }
        });
        javax.swing.GroupLayout jPanel_ConfLayout = new javax.swing.GroupLayout(jPanel_Conf);
        jPanel_Conf.setLayout(jPanel_ConfLayout);
        jPanel_ConfLayout.setHorizontalGroup(
            jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_ConfLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel_Conf_Title)
                .addGap(141, 141, 141))
            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_Conf_Logs, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel_ConfLayout.createSequentialGroup()
                        .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                                .addComponent(jLabel_Conf_Ip)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField_Conf_Ip, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel_Conf_Port)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField_Conf_Port, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                                .addComponent(jLabel_Conf_Login)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField_Conf_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton_Conf_Connect))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(jButton_Conf_Disconnect))
                            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel_Conf_Password)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPasswordField_Conf_Password, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jScrollPane_Conf_Logs, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel_ConfLayout.setVerticalGroup(
            jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ConfLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_Conf_Title)
                .addGap(28, 28, 28)
                .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Conf_Ip)
                    .addComponent(jTextField_Conf_Ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Conf_Login)
                    .addComponent(jTextField_Conf_Login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Conf_Password)
                    .addComponent(jPasswordField_Conf_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel_ConfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Conf_Port)
                    .addComponent(jTextField_Conf_Port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Conf_Connect)
                    .addComponent(jButton_Conf_Disconnect))
                .addGap(18, 18, 18)
                .addComponent(jLabel_Conf_Logs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane_Conf_Logs, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Configuration", jPanel_Conf);

        jTextArea_ChatMain_Main.setBackground(new java.awt.Color(230, 230, 230));
        jTextArea_ChatMain_Main.setColumns(20);
        jTextArea_ChatMain_Main.setEditable(false);
        jTextArea_ChatMain_Main.setRows(5);
        jScrollPane_ChatMain_Main.setViewportView(jTextArea_ChatMain_Main);
        DefaultCaret caret = (DefaultCaret)jTextArea_ChatMain_Main.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        jTextArea_ChatMain_Send.setColumns(20);
        jTextArea_ChatMain_Send.setRows(5);
        jScrollPane_ChatMain_Send.setViewportView(jTextArea_ChatMain_Send);

        jButton_SendChatMain.setText("Send");
        jButton_SendChatMain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ChatMain_SendActionPerformed(evt);
            }
        });

        jList_ChatMain_Members.setModel(new javax.swing.AbstractListModel() {
            String[] strings = {};
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        //jList_ChatMain_Members.setSize(50, 150);
        jScrollPane_ChatMain_Members.setViewportView(jList_ChatMain_Members);

        javax.swing.GroupLayout jPanel_ChatMainLayout = new javax.swing.GroupLayout(jPanel_ChatMain);
        jPanel_ChatMain.setLayout(jPanel_ChatMainLayout);
        jPanel_ChatMainLayout.setHorizontalGroup(
            jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ChatMainLayout.createSequentialGroup()
                .addGroup(jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel_ChatMainLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton_ChatMain_SendFile, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_ChatMainLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane_ChatMain_Main, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                            .addComponent(jScrollPane_ChatMain_Send))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane_ChatMain_Members, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton_SendChatMain))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel_ChatMainLayout.setVerticalGroup(
            jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ChatMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane_ChatMain_Members, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                    .addComponent(jScrollPane_ChatMain_Main))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton_ChatMain_SendFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel_ChatMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_ChatMainLayout.createSequentialGroup()
                        .addComponent(jScrollPane_ChatMain_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_ChatMainLayout.createSequentialGroup()
                        .addComponent(jButton_SendChatMain)
                        .addGap(24, 24, 24))))
        );

        jTabbed_Chat.addTab("Chat", jPanel_ChatMain);

        jTextArea_ChatWhisper_Main.setBackground(new java.awt.Color(230, 230, 230));
        jTextArea_ChatWhisper_Main.setColumns(20);
        jTextArea_ChatWhisper_Main.setEditable(false);
        jTextArea_ChatWhisper_Main.setRows(5);
        jScrollPane_ChatWhisper_Main.setViewportView(jTextArea_ChatWhisper_Main);

        jTextArea_ChatWhisper_Send.setColumns(20);
        jTextArea_ChatWhisper_Send.setRows(5);
        jScrollPane_ChatWhisper_Send.setViewportView(jTextArea_ChatWhisper_Send);

        jButton_ChatWhisper_SendFile.setText("Send a File");

        jButton_ChatWhisper_Send.setText("Send");

        //javax.swing.GroupLayout jPanel_WhisperLayout = new javax.swing.GroupLayout(jPanel_Whisper);
        //jPanel_Whisper.setLayout(jPanel_WhisperLayout);
        /*jPanel_WhisperLayout.setHorizontalGroup(
            jPanel_WhisperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                .addGroup(jPanel_WhisperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane_ChatWhisper_Main, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_WhisperLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel_WhisperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton_ChatWhisper_SendFile)
                            .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                                .addComponent(jScrollPane_ChatWhisper_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton_ChatWhisper_Send)))))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel_WhisperLayout.setVerticalGroup(
            jPanel_WhisperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                .addComponent(jButton_ChatWhisper_SendFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane_ChatWhisper_Main, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel_WhisperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jButton_ChatWhisper_Send))
                    .addGroup(jPanel_WhisperLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane_ChatWhisper_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );*/

        
        //jTabbed_Chat.addTab("test", jPanel_Whisper);

        javax.swing.GroupLayout jPanel_ChatLayout = new javax.swing.GroupLayout(jPanel_Chat);
        jPanel_Chat.setLayout(jPanel_ChatLayout);
        jPanel_ChatLayout.setHorizontalGroup(
            jPanel_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbed_Chat, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel_ChatLayout.setVerticalGroup(
            jPanel_ChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ChatLayout.createSequentialGroup()
                .addComponent(jTabbed_Chat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Chat", jPanel_Chat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    public void appendLogs(String msg){
    	jTextArea_Conf_Logs.append(msg+"\n");
    }
    
    public void appendChat(String msg){
    	jTextArea_ChatMain_Main.append(msg+"\n");
    }

    // Getters
    private String getIp(){
    	return jTextField_Conf_Ip.getText();
    }
    private String getPort(){
    	return jTextField_Conf_Port.getText();
    }
    protected String getLogin(){
    	return jTextField_Conf_Login.getText();
    }
    private String getPassword(){
    	return jPasswordField_Conf_Password.getText();
    }
    private String getMessage(){
    	return jTextArea_ChatMain_Send.getText();
    }
    
    // Events
    private void jButton_ChatMain_SendFileActionPerformed(java.awt.event.ActionEvent evt) throws FileNotFoundException{
    	
    	JFileChooser chooser = new JFileChooser();
    	chooser.showOpenDialog(new FileChooser());
    	File file = chooser.getSelectedFile();
    	try {
    		app.<File>sendAll(file);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    
    }
    
    public WhisperTab newTab(ChatClientApp app, GUI gui, String userDest)
    {
    	WhisperTab t = new WhisperTab(app, gui, userDest);
    	jTabbed_Chat.addTab(userDest, t);
    	
    	return t;
    }
    
    public WhisperTab getTab(String userDest) {
    	int index = jTabbed_Chat.indexOfTab(userDest);

    	if (index != -1)
    		return (WhisperTab)jTabbed_Chat.getComponentAt(index);
    	return null;
    }
    
    private void jList_ChatMain_MembersMouseListener(MouseEvent evt)
    {
    	int index = jList_ChatMain_Members.locationToIndex(evt.getPoint());
    	String userDest = jList_ChatMain_Members.getModel().getElementAt(index).toString();
    	newTab(this.app, this, userDest);
    	
    }
    
    
    private void jButton_Conf_ConnectActionPerformed(java.awt.event.ActionEvent evt) {
        try {
			
			new Client<ChatClientApp>(app, getIp()+":"+getPort(), "ChatServerApp");
			
			if(app.connect(getLogin(), getPassword()))
			{
				appendLogs("Connection reussie");
				this.switchLoginGUI(false);
			}
			else
			{
				appendLogs("Erreur lors de l'identification");
			}
			
			timer = new Timer();
			timer.schedule(new TimerTask(){
				public void run()
				{
					setConnectedList();
				}
			}, 0, 1000);
			
			
						
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			appendLogs("Probleme de connexion au serveur \n");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
    }
    
    protected void switchLoginGUI(boolean pEnabled) {
		this.jTextField_Conf_Ip.setEnabled(pEnabled);
		this.jTextField_Conf_Login.setEnabled(pEnabled);
		this.jTextField_Conf_Port.setEnabled(pEnabled);
		this.jPasswordField_Conf_Password.setEnabled(pEnabled);
		this.jButton_Conf_Connect.setEnabled(pEnabled);
		this.jButton_Conf_Disconnect.setEnabled(!pEnabled);
	}
    
 
    private void jButton_Conf_DisconnectActionPerformed(java.awt.event.ActionEvent evt) {
        try {
    		app.disconnect();
    		appendLogs("Deconnexion du serveur");
    		timer.cancel();
			this.switchLoginGUI(true);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
      
    }
    
    private void jButton_ChatMain_SendActionPerformed(java.awt.event.ActionEvent evt) {
    	try {
			app.sendAll(getLogin()+" dit : "+getMessage());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
    
    }
    
    public void setConnectedList(){
    	try {
			jList_ChatMain_Members.setListData(this.app.getServer().getConnectedList());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    //private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton_ChatWhisper_Send;
    private javax.swing.JButton jButton_ChatWhisper_SendFile;
    private javax.swing.JButton jButton_Conf_Connect;
    private javax.swing.JButton jButton_Conf_Disconnect;
    private javax.swing.JButton jButton_SendChatMain;
    private javax.swing.JButton jButton_ChatMain_SendFile;
    private javax.swing.JLabel jLabel_Conf_Ip;
    private javax.swing.JLabel jLabel_Conf_Login;
    private javax.swing.JLabel jLabel_Conf_Logs;
    private javax.swing.JLabel jLabel_Conf_Password;
    private javax.swing.JLabel jLabel_Conf_Port;
    private javax.swing.JLabel jLabel_Conf_Title;
    private javax.swing.JList jList_ChatMain_Members;
    private javax.swing.JPanel jPanel_Chat;
    private javax.swing.JPanel jPanel_ChatMain;
    private javax.swing.JPanel jPanel_Conf;
    //private javax.swing.JPanel jPanel_Whisper;
    private javax.swing.JPasswordField jPasswordField_Conf_Password;
    private javax.swing.JScrollPane jScrollPane_ChatMain_Main;
    private javax.swing.JScrollPane jScrollPane_ChatMain_Members;
    private javax.swing.JScrollPane jScrollPane_ChatMain_Send;
    private Timer timer;
    private javax.swing.JScrollPane jScrollPane_ChatWhisper_Main;
    private javax.swing.JScrollPane jScrollPane_ChatWhisper_Send;
    private javax.swing.JScrollPane jScrollPane_Conf_Logs;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbed_Chat;
    private javax.swing.JTextArea jTextArea_ChatMain_Main;
    private javax.swing.JTextArea jTextArea_ChatMain_Send;
    private javax.swing.JTextArea jTextArea_ChatWhisper_Main;
    private javax.swing.JTextArea jTextArea_ChatWhisper_Send;
    private javax.swing.JTextArea jTextArea_Conf_Logs;
    private javax.swing.JTextField jTextField_Conf_Ip;
    private javax.swing.JTextField jTextField_Conf_Login;
    private javax.swing.JTextField jTextField_Conf_Port;
    private ChatClientApp app = null;
    // End of variables declaration//GEN-END:variables
}
